package GUI;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;

import Client.BusinessImplements;
import Controler.Report_Controler;

import java.awt.Font;
import java.awt.SystemColor;
import java.awt.Color;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.border.MatteBorder;
import javax.swing.border.LineBorder;

public class Reports_panel extends Abstract_Jpanel {
	private JComboBox comboBox = new JComboBox();
	private JComboBox comboBox_1 = new JComboBox();;
	private JComboBox comboBox_2 = new JComboBox();;
	private String repNameActivity;
	private String repNameLending;
	private String repNameLates;
	private Showing_Report showR;
	
	private ArrayList<String> reportAnswer = new ArrayList<String>();
	/**
	 * Create the panel.
	 */
	public Reports_panel() {
		int i,size=0;
		File folder;
		File[] listOfFiles;
		String[] report1;
		
		//finding all files
		folder = new File("reports\\activity\\");
	    listOfFiles = folder.listFiles();
	    size=0;
	    for (i = 0; i < listOfFiles.length; i++)
	    	if (listOfFiles[i].isFile())
	    		size++;
	    report1 = new String[size]; 
	    for (i = 0; i < listOfFiles.length; i++)
	    	if (listOfFiles[i].isFile())
	    		comboBox.addItem(listOfFiles[i].getName())  ;
		setLayout(null);
		//finding all files
				folder = new File("reports\\lending\\");
			    listOfFiles = folder.listFiles();
			    size=0;
			    for (i = 0; i < listOfFiles.length; i++)
			    	if (listOfFiles[i].isFile())
			    		size++;
			    report1 = new String[size]; 

			    for (i = 0; i < listOfFiles.length; i++)
			    	if (listOfFiles[i].isFile())
			    		comboBox_1.addItem(listOfFiles[i].getName())  ;
		//finding all files
				folder = new File("reports\\lates\\");
			    listOfFiles = folder.listFiles();
			    size=0;
			    for (i = 0; i < listOfFiles.length; i++)
			    	if (listOfFiles[i].isFile())
			    		size++;
			    report1 = new String[size]; 
			    for (i = 0; i < listOfFiles.length; i++)
			    	if (listOfFiles[i].isFile())
			    		comboBox_2.addItem(listOfFiles[i].getName())  ;
		
		JLabel lblReports = new JLabel(" Reports");
		lblReports.setForeground(new Color(25, 25, 112));
		lblReports.setFont(new Font("MV Boli", Font.BOLD, 30));
		lblReports.setBounds(40, 11, 217, 33);
		add(lblReports);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new LineBorder(SystemColor.activeCaption, 6));
		panel_1.setBackground(SystemColor.inactiveCaption);
		panel_1.setBounds(50, 56, 951, 485);
		add(panel_1);
		panel_1.setLayout(null);
		
		JLabel lblReportsHistory = new JLabel("Reports History");
		lblReportsHistory.setBounds(32, 29, 169, 33);
		panel_1.add(lblReportsHistory);
		lblReportsHistory.setForeground(new Color(139, 0, 0));
		lblReportsHistory.setFont(new Font("MV Boli", Font.BOLD, 20));
		
		JPanel panel = new JPanel();
		panel.setBounds(32, 59, 577, 346);
		panel_1.add(panel);
		panel.setBorder(new MatteBorder(6, 6, 6, 6, (Color) new Color(245, 245, 220)));
		panel.setBackground(new Color(220, 220, 220));
		panel.setLayout(null);
		
		JLabel lblSelectReport = new JLabel("Activity Report:");
		lblSelectReport.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblSelectReport.setBounds(10, 57, 108, 20);
		panel.add(lblSelectReport);
		//comboBox = new JComboBox();
		comboBox.setBounds(128, 55, 225, 28);
		
						panel.add(comboBox);
						
						
						JLabel lblLendingReport = new JLabel("Lending Report:");
						lblLendingReport.setFont(new Font("Tahoma", Font.PLAIN, 14));
						lblLendingReport.setBounds(10, 153, 122, 28);
						panel.add(lblLendingReport);
						
						JLabel lblLatesReport = new JLabel("Lates Report:");
						lblLatesReport.setFont(new Font("Tahoma", Font.PLAIN, 14));
						lblLatesReport.setBounds(10, 271, 122, 23);
						panel.add(lblLatesReport);
						
						//comboBox_1 = new JComboBox();
						comboBox_1.setBounds(128, 153, 225, 28);
						panel.add(comboBox_1);
						
								//comboBox_2 = new JComboBox();
								comboBox_2.setBounds(128, 266, 225, 28);
								panel.add(comboBox_2);
								
								JButton btnGo_1 = new JButton("Select Report");
								btnGo_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
								btnGo_1.addActionListener(new ActionListener() {
									public void actionPerformed(ActionEvent arg0) {
										Report_Controler rpc = new Report_Controler();
										repNameActivity = getActName();
										reportAnswer = rpc.seeActivityReport(repNameActivity);
										on_screen_unload();
										showR = new Showing_Report();
										BusinessImplements.GetInstance().getMainFrame().setContentPane(showR);
										showR.setSize(getMaximumSize());
										showR.displayReport(reportAnswer);
										showR.on_screen_load();
									}
								});
								btnGo_1.setBounds(395, 50, 140, 34);
								panel.add(btnGo_1);
								
								JButton btnSelectRepory = new JButton("Select Report");
								btnSelectRepory.setFont(new Font("Tahoma", Font.PLAIN, 14));
								btnSelectRepory.addActionListener(new ActionListener() {
									public void actionPerformed(ActionEvent arg0) {
										Report_Controler rpc = new Report_Controler();
										repNameLending = getLendName();
										reportAnswer = rpc.seeLendingReport(repNameLending);
										on_screen_unload();
										showR = new Showing_Report();
										BusinessImplements.GetInstance().getMainFrame().setContentPane(showR);
										showR.setSize(getMaximumSize());
										showR.displayReport(reportAnswer);
										showR.on_screen_load();
									}

									
								});
								btnSelectRepory.setBounds(395, 148, 140, 34);
								panel.add(btnSelectRepory);
								
								JButton btnSelectRepory_1 = new JButton("Select Report");
								btnSelectRepory_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
								btnSelectRepory_1.addActionListener(new ActionListener() {
									public void actionPerformed(ActionEvent e) {

										Report_Controler rpc = new Report_Controler();
										repNameLates = geLateName();
										reportAnswer = rpc.seeLatesReport(repNameLates);
										on_screen_unload();
										showR = new Showing_Report();
										BusinessImplements.GetInstance().getMainFrame().setContentPane(showR);
										showR.setSize(getMaximumSize());
										showR.displayReport(reportAnswer);
										showR.on_screen_load();
									}

									
								});
								btnSelectRepory_1.setBounds(395, 260, 140, 34);
								panel.add(btnSelectRepory_1);
								
								JPanel panel_2 = new JPanel();
								panel_2.setBounds(633, 59, 292, 346);
								panel_1.add(panel_2);
								panel_2.setBorder(new MatteBorder(6, 6, 6, 6, (Color) new Color(245, 245, 245)));
								panel_2.setBackground(new Color(220, 220, 220));
								panel_2.setLayout(null);
								
								JButton btnGenerateActivityReport = new JButton("Generate Activity Report");
								btnGenerateActivityReport.setBounds(27, 50, 226, 47);
								panel_2.add(btnGenerateActivityReport);
								btnGenerateActivityReport.setFont(new Font("Tahoma", Font.PLAIN, 14));
								
								JButton btnGenerateLendingReport = new JButton("Generate Lending Report");
								btnGenerateLendingReport.setBounds(27, 142, 223, 47);
								panel_2.add(btnGenerateLendingReport);
								btnGenerateLendingReport.setFont(new Font("Tahoma", Font.PLAIN, 14));
								
								JButton btnGenerateLatesReport = new JButton("Generate Lates Report");
								btnGenerateLatesReport.setBounds(27, 245, 226, 47);
								panel_2.add(btnGenerateLatesReport);
								btnGenerateLatesReport.setFont(new Font("Tahoma", Font.PLAIN, 14));
								
								JLabel lblNewLabel = new JLabel("Create a Current Report");
								lblNewLabel.setBounds(634, 30, 307, 30);
								panel_1.add(lblNewLabel);
								lblNewLabel.setForeground(new Color(128, 0, 0));
								lblNewLabel.setBackground(new Color(128, 0, 0));
								lblNewLabel.setFont(new Font("MV Boli", Font.BOLD, 20));
		btnGenerateLatesReport.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Report_Controler rpc = new Report_Controler();
				
				reportAnswer = rpc.generateLatesReport();
				on_screen_unload();
				showR = new Showing_Report();
				BusinessImplements.GetInstance().getMainFrame().setContentPane(showR);
				showR.setSize(getMaximumSize());
				showR.displayReport(reportAnswer);
				showR.on_screen_load();
			}
		});
		btnGenerateLendingReport.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Report_Controler rpc = new Report_Controler();
				
				reportAnswer = rpc.generateLendingReport();
				on_screen_unload();
				showR = new Showing_Report();
				BusinessImplements.GetInstance().getMainFrame().setContentPane(showR);
				showR.setSize(getMaximumSize());
				showR.displayReport(reportAnswer);
				showR.on_screen_load();
	
			}
		});
		btnGenerateActivityReport.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Report_Controler rpc = new Report_Controler();
				
				reportAnswer = rpc.generateActiveReport();
				on_screen_unload();
				showR = new Showing_Report();
				BusinessImplements.GetInstance().getMainFrame().setContentPane(showR);
				showR.setSize(getMaximumSize());
				showR.displayReport(reportAnswer);
				showR.on_screen_load();
				
			}
		});
		
	

	}
	
	private String getActName() {
		String str =(String) comboBox.getSelectedItem();
		str = str.substring(0, (((String) comboBox.getSelectedItem()).lastIndexOf("x"))-2 );
		return str;
	}
	private String getLendName() {
		String str =(String) comboBox_1.getSelectedItem();
		str = str.substring(0, (((String) comboBox_1.getSelectedItem()).lastIndexOf("x"))-2 );
		return str;
	}
	private String geLateName() {
		String str =(String) comboBox_2.getSelectedItem();
		str = str.substring(0, (((String) comboBox_2.getSelectedItem()).lastIndexOf("x"))-2 );
		return str;
	}
	
	
	

	@Override
	public void next_screen() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void prev_screen() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void on_screen_load() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void on_screen_unload() {
		// TODO Auto-generated method stub
		
	}
}
